//
//  MYGFindListController.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/3/11.
//
import UIKit
import SwifterSwift
//import CollectionViewWaterfallLayout
import JXSegmentedView
import MJRefresh

class MYGFindListController: MYGBaseController {
    private var page = 1
    var type = 0
    
    var hidNav = true
    var keyword: String = "" {
        didSet {
            if oldValue == keyword {
                return
            }
            self.tableView.mj_header?.beginRefreshing()
        }
    }
    private var contents: [MYGFindListModel] = []
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white
        view.addSubview(tableView)
        
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        tableView.mj_header?.beginRefreshing()

    }
    
    private lazy var tableView: UITableView = {
        let tableV = UITableView()
        tableV.backgroundColor = .white
        tableV.delegate = self
        tableV.dataSource = self
        tableV.separatorStyle = .none
        tableV.showsVerticalScrollIndicator = false
        tableV.register(cellWithClass: MYGFindTableCell.self)
        tableV.mj_header = MJRefreshNormalHeader {[weak self] in
            self?.page = 1
            self?.loadData()
        }
        
        tableV.mj_footer = MJRefreshAutoNormalFooter{[weak self] in
            self?.page += 1
            self?.loadData()
        }
        return tableV
    }()
    
    override var hideNavigationController: Bool {
        hidNav
    }
}

extension MYGFindListController: JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        view
    }
}

extension MYGFindListController:UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        contents.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 386
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withClass: MYGFindTableCell.self, for: indexPath)
        cell.changeAction = {[weak self] in
            self!.tableView.reloadData()
        }
        cell.model = contents[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = MYGFindDetailController(id: contents[indexPath.row].id)
        UIViewController.currentViewController?.navigationController?.pushViewController(vc)
    }
    
}

extension MYGFindListController {
    private func loadData() {
        
        var keywords: String? = keyword
        if keyword.isEmpty {
            keywords = nil
        }
        print("发现页面接口类型list_type=1自己或指定人 2我关注的 3我的好友 4推荐->\(type)")
        self.contents.removeAll()
        MMYGNetwork<MYGFindAPI, MYGPageModel<MYGFindListModel>>.request(target: .list(type: 0, list_type: type, page: page, keywords: keywords)) { item in
            guard let item = item else {
                return
            }
            
            self.tableView.mj_header?.endRefreshing()
            
            if item.page.page == 1 {
                self.contents = item.list
            } else {
                self.contents.append(contentsOf: item.list)
            }
            
            if item.page.total == self.contents.count {
                self.tableView.mj_footer?.endRefreshingWithNoMoreData()
            } else {
                self.tableView.mj_footer?.endRefreshing()
            }
            
            self.tableView.reloadData()
        } failed: { _, error in
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
            self.page -= 1
            self.page = self.page > 0 ? self.page : 1
            MMYGHUD.showToast(text: error)
        }
    }
}
